package com.example.braden.answergame.base;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
//import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

import java.io.InputStream;

import okhttp3.OkHttpClient;

/**
 * Created by braden on 2018/8/11.
 */
@GlideModule
public class GameGlideModule extends AppGlideModule {
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        builder.setLogLevel(Log.WARN);

//======================================================
        int memoryCacheSizeBytes = calculateMemoryCacheSize(context); // 20mb
        builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));

//        MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
//                .setMemoryCacheScreens(2)
//                .build();
//        builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));


//======================================================
////        int bitmapPoolSizeBytes = 1024 * 1024 * 30; // 30mb
////        builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));
//
//        calculator = new MemorySizeCalculator.Builder(context)
//                .setBitmapPoolScreens(3)
//                .build();
//        builder.setBitmapPool(new LruBitmapPool(calculator.getBitmapPoolSize()));


//======================================================
        int diskCacheSizeBytes = 1024 * 1024 * 30;  //30 MB
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));


//======================================================
        RequestOptions defaultOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            defaultOptions = defaultOptions.disallowHardwareConfig();
        }

        builder.setDefaultRequestOptions(defaultOptions);
//        ViewTarget.setTagId(R.id.id_glide_image_target);
        super.applyOptions(context, builder);
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull
            Registry registry) {
        /* GIFs */
//        registry.prepend(
//                Registry.BUCKET_GIF,
//                InputStream.class,
//                pl.droidsonroids.gif.GifDrawable.class,
//                new MyStreamGifDecoder(registry.getImageHeaderParsers(), glide.getArrayPool()));

//        OkHttpClient client = new OkHttpClient();
//        registry.replace(GlideUrl.class, InputStream.class,
//                new OkHttpUrlLoader.Factory(client));
        super.registerComponents(context, glide, registry);
    }

    private static int calculateMemoryCacheSize(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        boolean largeHeap = (context.getApplicationInfo().flags & ApplicationInfo
                .FLAG_LARGE_HEAP) != 0;
        int memoryClass = am.getMemoryClass();
        if (largeHeap && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            memoryClass = am.getLargeMemoryClass();
        }
        // Target ~15% of the available heap.
        int byteSize = 1024 * 1024 * memoryClass / 7;
        int max = 15 * 1024 * 1024;// 限制最大 15M
        byteSize = Math.min(max, byteSize);
        return byteSize;
    }
}
